<template>
  <div>
    <el-dialog
      :model-value="open"
      :close-on-click-modal="false"
      :title="title"
      :before-close="handleClose"
      append-to-body
    >
      <el-form
        ref="setFormRef"
        :model="setForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="国家代码" prop="countryCode" :label-position="labelPosition">
          <el-input
            v-model="setForm.countryCode"
            placeholder="请输入国家代码"
            clearable
          />
        </el-form-item>
        <el-form-item label="国家中文名称" prop="countryChName" :label-position="labelPosition">
          <el-input
            v-model="setForm.countryChName"
            placeholder="请输入国家中文名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="国家英文名称" prop="countryEnName" :label-position="labelPosition">
          <el-input
            v-model="setForm.countryEnName"
            placeholder="请输入国家英文名称"
            clearable
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, defineExpose } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { addCountry, editCountry, getCountryDetail } from '@/api/basicConfig/stateMaintanance'

const props = defineProps({
  open: {
    type: Boolean,
  },
  title: {
    type: String,
    default: '',
  },
})
const 
  setForm = ref({
    countryCode: undefined,
    countryChName: undefined,
    countryEnName: undefined,
  }),
  rules = ref({
    countryCode: [{ required: true, message: '必填', trigger: 'blur' }],
    countryChName: [{ required: true, message: '必填', trigger: 'blur' }],
    countryEnName: [{ required: true, message: '必填', trigger: 'blur' }],
  }),
  labelPosition = ref('right'),
  setFormRef = ref(null),
  {
    proxy,
  }:{
   proxy:any
 } = getCurrentInstance()

const emit = defineEmits(['changeList', 'update:open'])
const reset = () => {
  setForm.value = {
    countryCode: undefined,
    countryChName: undefined,
    countryEnName: undefined,
  }
  proxy.$resetForm(setFormRef.value)
}
const submit = () => {
  proxy.$validateForm(setFormRef.value, async () => {
    let res = null
    if (props.title.includes('修改')) {
      res = await editCountry(setForm.value)
    } else {
      res = await addCountry(setForm.value)
    }
    if (res.code == 200) {
      ElMessage.success(`${ props.title }成功`)
      emit('update:open', false)
    } else {
      ElMessage.error(`${ props.title }失败`)
    }
  })
}
const cancel = () => {
  reset()
  emit('update:open', false)
}
const handleClose = () => {
  reset()
  emit('update:open', false)
}
const getDetail = (id) => {
  getCountryDetail(id).then((res) => {
    if (res.code == 200) {
      setForm.value = res.data
    }
  })
}

defineExpose({
  getDetail,
})
</script>
l
